package com.analyse.stock.strategy.indicator;

import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.indicators.helpers.*;
import org.ta4j.core.num.Num;

public class LWRIndicator implements Indicator<Num> {

    private final Indicator<Num> closePrice; // 收盘价指标
    private final HighestValueIndicator highestHigh; // 最高值指标
    private final LowestValueIndicator lowestLow; // 最低值指标
    private final Integer barCount;
    /**
     * 构造 慢速威廉指标 LWR
     * @param closePriceIndicator 收盘价指标
     * @param highPriceIndicator  最高价指标
     * @param lowPriceIndicator   最低价指标
     * @param rsvPeriod            rsv 周期长度
     */
    public LWRIndicator(Indicator<Num> closePriceIndicator,
                        Indicator<Num> highPriceIndicator,
                        Indicator<Num> lowPriceIndicator,
                        int rsvPeriod) {
        this.closePrice = closePriceIndicator;
        this.highestHigh = new HighestValueIndicator(highPriceIndicator, rsvPeriod);
        this.lowestLow = new LowestValueIndicator(lowPriceIndicator, rsvPeriod);
        this.barCount = rsvPeriod;
    }

    public LWRIndicator(BarSeries series,int rsvPeriod) {
        this(new ClosePriceIndicator(series),new HighPriceIndicator(series),new LowPriceIndicator(series),rsvPeriod);
    }

    @Override
    public Num getValue(int index) {
        Num highestHighValue = highestHigh.getValue(index);
        Num lowestLowValue = lowestLow.getValue(index);
        Num closeValue = closePrice.getValue(index);
        // LWR 计算公式
        return closeValue.minus(highestHighValue)
                .dividedBy(highestHighValue.minus(lowestLowValue))
                .multipliedBy(closeValue.numOf(-100)); // 注意乘以 -100
    }

    @Override
    public org.ta4j.core.BarSeries getBarSeries() {
        return closePrice.getBarSeries();
    }

    @Override
    public Num numOf(Number number) {
        return null;
    }
}

